import * as THREE from "three";
import { Config } from "./Config";

export class Cache {

  static COIN_MESH: THREE.Mesh = null as any;
  static PANEL_GEO: THREE.BufferGeometry = null as any;
  static NORMAL_COLOR_MAT = new THREE.MeshPhongMaterial({ map: new THREE.TextureLoader().load(`./upload/c1.png`) });
  static CHOOSE_COLOR_MAT = new THREE.MeshPhongMaterial({ color: 0xff0000 });
  static CARD_MESH_MAP: Record<any, any> = {};
  static CARD_MAT_MAP: Record<any, any> = {};

  constructor() {

  }

  static init() {
    const geometry = new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH);

    const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 128];
    nums.forEach((i) => {
      const textureLoader = new THREE.TextureLoader();
      // const type = i > 100 ? 32 : i + 12;
      const texture = textureLoader.load(`./upload/${i}.png`);
      Cache.CARD_MAT_MAP[i] = texture;
      Cache.CARD_MESH_MAP[i] = new THREE.Mesh(geometry.clone(), new THREE.MeshBasicMaterial({ map: texture }));
    });
  }

  static getCoin(): THREE.Mesh {
    return Cache.COIN_MESH.clone() as THREE.Mesh;
  }

  static getMesh(type: any): THREE.Mesh {
    return Cache.CARD_MESH_MAP[type].clone() as THREE.Mesh;
  }

  static getNormalMat(): THREE.MeshPhongMaterial {
    return Cache.NORMAL_COLOR_MAT.clone() as THREE.MeshPhongMaterial
  }

  static getChooseMat(): THREE.MeshPhongMaterial {
    return Cache.CHOOSE_COLOR_MAT.clone() as THREE.MeshPhongMaterial
  }

  static getPanelGeo(): THREE.BufferGeometry {
    return Cache.PANEL_GEO.clone() as THREE.BufferGeometry
  }

}